/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    May 2023
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/

/*==================================================
            0: Program set up
==================================================*/
*Written on STATA 17
drop _all
set varabbrev off	// no variable abbreviations allowed (personal preference)

** source dir
cd "${dir4r}" // graph dir

/*==================================================
            1: load and transformations
==================================================*/

*----------  1.1. Data prep:
use "${dir3r}/01_survey/survey_targeting_r1_long.dta", clear

*----------  1.1.2 Estimate PMT for survey data:

*------------ poverty line
scalar epovline = 137350
scalar l_epovline = log(epovline)

scalar povline = 327674
scalar l_povline = log(povline)

*----------- dummys of category 


* Actual extr. poor
gen epoor = l_pp_inc_srv < l_epovline
replace epoor = . if l_pp_inc_srv == .

gen poor = l_pp_inc_srv < l_povline
replace poor = . if l_pp_inc_srv == .


g work_b=work_p if year==2019
egen aux1=max(work_b), by(id_vivienda_SISBEN)

g work_post=work_p if year==2020
egen aux2=max(work_post), by(id_vivienda_SISBEN)

g job_loss1y=aux2==0 & aux1==1
label var job_loss1y "Job disruption"
egen shock_aux= rowmax(ilness death divorce bankrupt theft conflict cut_remittance fire nat_disaster)
replace shock_aux=. if year!=2020
egen any_shock_nl=max(shock_aux), by(id_vivienda_SISBEN)
label var any_shock_nl "Shock (non labor)"
gen Post=year>2019
label var Post "Post 2019"
label var l_pp_inc_srv "Log Per-capita Income"
label var epoor "Extreme Poverty"
label var foodsec "Went Hungry"

g PostXJob=Post*job_loss1y
label var PostXJob "Post X Job Disruption"
g PostXshock=Post*any_shock_nl
label var PostXshock "Post X Shock (non labor)"

egen assets=rowtotal(assets_own_*)
label var assets "Asset ownership index"

reghdfe l_pp_inc_srv PostXJob , a(id_vivienda_SISBEN year) cl(id_vivienda_SISBEN)
outreg2 using "T2_diffindiff.tex", replace label tex(frag)  keep(PostXJob)

reghdfe epoor PostXJob , a(id_vivienda_SISBEN year) cl(id_vivienda_SISBEN)
outreg2 using "T2_diffindiff.tex", append label tex(frag)  keep(PostXJob)

reghdfe foodsec PostXJob , a(id_vivienda_SISBEN year) cl(id_vivienda_SISBEN)
outreg2 using "T2_diffindiff.tex", append label tex(frag)  keep(PostXJob)

reghdfe l_pp_inc_srv PostXshock, a(id_vivienda_SISBEN year) cl(id_vivienda_SISBEN)
outreg2 using "T2_diffindiff.tex", append label tex(frag)  keep(PostXshock)

reghdfe epoor PostXshock, a(id_vivienda_SISBEN year) cl(id_vivienda_SISBEN)
outreg2 using "T2_diffindiff.tex", append label tex(frag)  keep(PostXshock)

reghdfe foodsec PostXshock, a(id_vivienda_SISBEN year) cl(id_vivienda_SISBEN)
outreg2 using "T2_diffindiff.tex", append label tex(frag)  keep(PostXshock)

